"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix_yc = "YCD" + formatDate(new Date(), "yyyyMMdd");
var str_prefix_hy = "HYD" + formatDate(new Date(), "yyyyMMdd");

describe("message", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
    });

    after(function() {
        clearDB();
    });

    it("创建车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "苏A123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "2.0",
            "year": "2008",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("创建用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

    });

    it("通过用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
    });

    it("消息列表", function() {
        var result = httpPost("POST", "/v1.0/api/message/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].m_type, "approval");
        assert.equal(result.data.list[0].status, "未读");
        assert.equal(result.data.list[0].bind_type, "vehicleRecord");
        assert.equal(result.data.list[0].vehicleRecord_no, str_prefix_yc + "001");
    });

    it("已读用车单", function() {
        var result = httpPost("POST", "/v1.0/api/message/Read", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        var result = httpPost("POST", "/v1.0/api/message/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].m_type, "approval");
        assert.equal(result.data.list[0].status, "已读");
    });

    it("创建会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Create", {
            "name": "会议室101",
            "load": 20,
            "building": "中心楼",
            "floor": 1,
            "device": "白板,空调,会议桌椅",
            "remark": "隔音不好",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("创建会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("通过会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
    });

    it("消息列表", function() {
        var result = httpPost("POST", "/v1.0/api/message/List", {
            "status": "未读",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].m_type, "approval");
        assert.equal(result.data.list[0].bind_type, "meetingRecord");
        assert.equal(result.data.list[0].status, "未读");
    });

    it("已读会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/message/Read", {
            "id": 2,
        })
        assert.equal(result.code, "200");
        var result = httpPost("POST", "/v1.0/api/message/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].m_type, "approval");
        assert.equal(result.data.list[0].status, "已读");
    });
});

require('test').run();